package com.example.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.admin.SysDeptRole;
import com.example.admin.SysPermission;
import com.example.admin.SysRole;
import com.example.admin.SysRolePermission;
import com.example.admin.mapper.SysRoleMapper;
import com.example.admin.service.ISysDeptRoleService;
import com.example.admin.service.ISysRolePermissionService;
import com.example.admin.service.ISysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 系统角色信息表 服务实现类
 * </p>
 *
 * @author wangkai
 * @since 2019-06-04
 */
@Service("sysRoleService")
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements ISysRoleService {

    @Autowired
    private ISysRolePermissionService rpService;

    @Autowired
    private ISysDeptRoleService dpService;

    @Override
    @Transactional(readOnly = false, rollbackFor = {Exception.class, RuntimeException.class})
    public boolean saveRole(SysRole sysRole) {
        if (sysRole.insert()) {
            if (sysRole.getId() == null) {
                throw new RuntimeException();
            } else {
                List<SysPermission> list = sysRole.getPermissions();
                if (list != null && list.size() > 0) {
                    for (SysPermission sysPermission : list) {
                        SysRolePermission rp = new SysRolePermission();
                        rp.setRoleId(sysRole.getId());
                        rp.setPermissionId(sysPermission.getId());
                        if (!rp.insert()) {
                            throw new RuntimeException();
                        }
                    }
                }
            }
        }
        return true;
    }

    @Override
    @Transactional(readOnly = false, rollbackFor = {Exception.class, RuntimeException.class})
    public boolean updateRole(SysRole sysRole) {
        QueryWrapper<SysRolePermission> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_id", sysRole.getId());
        if (sysRole.insertOrUpdate()) {
            if (sysRole.getId() == null) {
                throw new RuntimeException();
            } else {
                List<SysPermission> list = sysRole.getPermissions();
                if (list != null && list.size() > 0) {
                    if (rpService.remove(queryWrapper)) {
                        for (SysPermission sysPermission : list) {
                            SysRolePermission rp = new SysRolePermission();
                            rp.setRoleId(sysRole.getId());
                            rp.setPermissionId(sysPermission.getId());
                            if (!rp.insert()) {
                                throw new RuntimeException();
                            }
                        }
                    }
                }
                List<SysDeptRole> deptRoles = sysRole.getDeptRoles();
                if (deptRoles != null && deptRoles.size() > 0) {
                    QueryWrapper<SysDeptRole> queryWrapperr = new QueryWrapper<>();
                    queryWrapper.eq("role_id", sysRole.getId());
                    if (dpService.remove(queryWrapperr)) {
                        deptRoles.forEach(info -> {
                            if (!info.insert()) throw new RuntimeException();
                        });
                    }
                }
            }
        }
        return true;
    }

    @Override
    @Transactional(readOnly = false, rollbackFor = {Exception.class, RuntimeException.class})
    public boolean deleteRole(List<Integer> ids) {
        if (this.removeByIds(ids)) {
            QueryWrapper<SysRolePermission> queryWrapper = new QueryWrapper<>();
            queryWrapper.in("role_id", ids);
            if (!rpService.remove(queryWrapper)) {
                throw new RuntimeException();
            }
            QueryWrapper<SysDeptRole> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.in("role_id", ids);
            dpService.remove(queryWrapper1);
        }
        return true;
    }

    @Override
    public List<SysRole> getRoleList(Integer userId) {
        return this.baseMapper.getRoleList(userId);
    }

    @Override
    public SysRole getRole(Integer roleId) {
        return this.baseMapper.getRole(roleId);
    }

}
